package com.ljp.authorization.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //这个是结合security自定义登录页做的，可以忽略，是个人保留参考用
//        http.authorizeRequests()
//                .antMatchers("/login", "/webjars/**", "/logout").permitAll()
//                .antMatchers("/super/**").hasRole("SUPER_ADMIN")
//                .antMatchers("/admin/**").hasAnyRole("ADMIN", "SUPER_ADMIN")
//                .anyRequest().authenticated()
//                .and().formLogin().loginPage("/login").permitAll()
//                .and().logout().permitAll().invalidateHttpSession(true)
//                .and().csrf().disable();
        http.httpBasic()
                .and()
                .csrf().disable();
    }

    //这个类在com.ljp.authorization.security.MyAuthorizationServerConfig有进行注入，所有在这里注册成bean
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    //可用这个方法向内存加入账户，密码，角色，不必写个UserDetailService类来进行验证登录
    //    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("ljp1").password(passwordEncoder.encode("123")).roles("USER")
//                .and()
//                .withUser("ljp2").password(passwordEncoder.encode("123")).roles("USER","ADMIN")
//                .and()
//                .withUser("ljp3").password(passwordEncoder.encode("123")).roles("SUPER_ADMIN");
//    }


}
